package com.cxh.rmsq.net.newNet

import android.os.Build
import com.blankj.utilcode.util.AppUtils
import com.cxh.rmsq.SealApp
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.net.SealTalkUrl
import com.cxh.rmsq.utils.getHttpLoggingInterceptor
import io.rong.callkit.util.SPUtils
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Created by 赵杰 on 2020-01-06.
 */

object ApiServiceClient {

    val apiService by lazy {
        return@lazy Retrofit.Builder()
            .client(OkHttpClient()
                .newBuilder()
                .addInterceptor(AppHttpInterceptor())
                .addInterceptor(getHttpLoggingInterceptor())
                .addInterceptor { chain ->
                    return@addInterceptor chain.proceed(
                        chain
                            .request()
                            .newBuilder()
//                            .addHeader("Authorization", Constant.TOKEN)
                            .addHeader("Authorization", SPUtils.get(SealApp.getApplication(), "accessToken", "").toString())
                            .addHeader("tenant-id", "1")
                            .addHeader("device-brand", Build.BRAND)
                            .addHeader("app-version", AppUtils.getAppVersionName())
                            .addHeader("device-version", AppUtils.getAppVersionName())
//                            .addHeader("Connection", "close")
                            .build()
                    )
                }
                .readTimeout(Constant.READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(Constant.READ_TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(Constant.CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(SealTalkUrl.DOMAIN)
            .build()
            .create(ApiService::class.java)
    }


}